(function() {
	var u = navigator.userAgent;
	var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
	if (isIOS) {
		if (screen.height == 812 && screen.width == 375) {
			//是iphoneX
			$('#nullk').show();
		};
	};
	var state = UrlParm.parm('params'); //传递的参数
	var info = null; //文案详情
	var articleid = null; //文案标识
	var shareid = null; //文案分享者
	var pageScene = 0; //0-用户查看 1-分享查看
	var userInfo = null;
	var isbj = false; //是否编辑界面进入
	var mfhcwoFlg = false; //免费换成我的是否成功
	var mfhcwoPost = false; //换成我的是否请求中
	if (state) {
		var data = JSON.parse(state);
		pageScene = data.type;
		articleid = data.articleid || null;
		shareid = data.shareid || null;
		isbj = data.isbj ? data.isbj : false;
		//请求详情接口
		getDetail();
	};

	/**
	 * 判断编辑、换成我的、关注按钮显示
	 */
	function btnShowInit() {
		if (userInfo) {
			if (userInfo.collectSubscribe != 1) { //未采集关注信息
				//采集关注信息
				focusCJ(userInfo.userId, function(res) {
					if (res.result.success) {
						userInfo.collectSubscribe = 1;
						userInfo.subscribe = res.data.subscribe ? 1 : 0;
						//设置用户缓存
						set_cache('userInfo', userInfo);
						if (res.data.subscribe) {
							//已关注
							if (pageScene == 0) { //0-用户查看
								$('#wa_fb').html('编辑文案');
							} else if (pageScene == 1) { // 1-分享查看
								if (info && info.article && info.article.userAccountId == userInfo.id) {
									$('#wa_fb').html('编辑文案');
								} else {
									$('#wa_fb').html('免费换成我的文案');
								}
							};
						};
						$('#wa_fb').show();
					};
				}, function() {});
			} else {
				if (pageScene == 0) { //0-用户查看
					$('#wa_fb').html('编辑文案');
				} else if (pageScene == 1) { // 1-分享查看
					if (info && info.article && info.article.userAccountId == userInfo.id) {
						$('#wa_fb').html('编辑文案');
					} else {
						$('#wa_fb').html('免费换成我的文案');
					}
				};
				$('#wa_fb').show();
			};
		};
	};

	//微信逻辑----初始化
	if (isWeiXin()) {
		//判断是否已授权
		userInfo = get_cache('userInfo') || null;
		if (userInfo && userInfo != '' && userInfo != 'undefined') {
			$('#tolist').show();
			//刷新用户信息
			sxUserInfo(userInfo.userId, function(res) {
				if (res.result.success && res.data) {
					userInfo = res.data;
					//设置用户缓存
					set_cache('userInfo', res.data);
					//判断编辑、换成我的、关注按钮显示
					btnShowInit();
				};
			}, null);

		} else {
			// 未授权，获取用户信息
			var code = UrlParm.parm('code');
			if (code) {
				var url = jkMap.authorize + '?code=' + code;
				if (shareid) {
					//通过授权进入
					url += '&agent=' + shareid;
				};
				//授权获取用户信息
				postF(url, undefined, 'get', function(res) {
					//成功之后的处理
					if (res.result.success) {
						$('#tolist').show();
						userInfo = res.data;
						//设置用户缓存
						set_cache('userInfo', res.data);
						//判断编辑、换成我的、关注按钮显示
						btnShowInit();
					} else {
						//提示层
						layer.msg(res.result.message, {
							time: 2000
						});
					};
				}, function() {});
			} else {
				var wxlognnum = get_cache('wxlognnum'); //登录请求次数
				if (!wxlognnum || wxlognnum <= 10) {
					var a = setTimeout(function() {
						clearTimeout(a);
						set_cache('wxlognnum', wxlognnum ? wxlognnum++ : 1);
						//跳转授权
						wxlogn(ymym + 'article.html', state);
					}, 1000);
				};
			};
		};

		if (isbj) {
			//分享提示弹窗
			$('#shareTs').show();
		};

		//js-api config注入
		jsconfig(window.location.href, function(res) {
			if (res.result.success && res.data) {
				wx.config({
					debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
					appId: res.data.appId, // 必填，公众号的唯一标识
					timestamp: res.data.timestamp, // 必填，生成签名的时间戳
					nonceStr: res.data.nonceStr, // 必填，生成签名的随机串
					signature: res.data.signature, // 必填，签名
					jsApiList: [
						'updateAppMessageShareData',
						'updateTimelineShareData',
						'hideMenuItems',
						'showMenuItems'
					] // 必填，需要使用的JS接口列表
				});
			};
		});
		wx.ready(function() {
			//隐藏菜单
			wx.hideMenuItems({
				menuList: [
					'menuItem:share:appMessage',
					'menuItem:share:timeline',
					'menuItem:share:qq',
					'menuItem:share:weiboApp',
					'menuItem:favorite',
					'menuItem:share:facebook',
					'menuItem:share:QZone',
					'menuItem:copyUrl',
					'menuItem:originPage',
					'menuItem:openWithQQBrowser',
					'menuItem:editTag',
					'menuItem:delete',
					'menuItem:readMode',
					'menuItem:openWithSafari',
					'menuItem:share:email',
				] // 要隐藏的菜单项，只能隐藏“传播类”和“保护类”按钮，所有menu项见附录3
			});

			var shn = 50;
			var aaa = setInterval(function() {
				if (info) {
					clearInterval(aaa);
					wx.showMenuItems({
						menuList: [
							'menuItem:share:appMessage',
							'menuItem:share:timeline',
						] // 要显示的菜单项，所有menu项见附录3
					});
					var data = {
						articleid: info.article.articleId,
						shareid: info.user.userId + '',
						type: 1
					};
					// 分享给朋友
					wx.updateAppMessageShareData({
						title: info.article.title, // 分享标题
						desc: info.article.description, // 分享描述
						link: ymym + "article.html?params=" + encodeURIComponent(JSON.stringify(data)),
						imgUrl: info.article.cover, // 分享图标
						success: function() {
							// 设置成功
						}
					});

					//分享到朋友圈
					wx.updateTimelineShareData({
						title: info.article.title, // 分享标题
						link: ymym + "article.html?params=" + encodeURIComponent(JSON.stringify(data)),
						imgUrl: info.article.cover, // 分享图标
						success: function() {
							// 设置成功
						}
					});
				} else {
					if (shn <= 0) {
						clearInterval(aaa);
					};
					shn--;
				};
			}, 100);

		});
	} else {
		$('#wa_fb').hide();
		$('#tolist').hide();
	};

	/**
	 * 查看文案详情
	 * @param {Object} articleid 文案id
	 */
	function getDetail() {
		if (articleid) {
			getArticleDetail(articleid, function(res) {
				if (res.result.success && res.data) {
					info = res.data;
					if (info.callCard) {
						//用户信息回显
						if (info.callCard.headImg) {
							$('.u_headimg').attr("src", info.callCard.headImg);
						};
						if (info.callCard.nickname) {
							$(".u_nickname").html(info.callCard.nickname);
						};
					} else {
						var num = 0;
						var seta = setInterval(function() {
							if (userInfo || num > 50) {
								clearInterval(seta);
								if (info.user.userId == userInfo.userId) {
									var bw = $('body').width();
									layer.msg('文章信息不完整，请前往编辑界面补充', {
										skin: 'gztc-class',
										area: Number(bw * 0.7) + 'px',
										time: 0, //不自动关闭							
										btn: ['前往', '取消'],
										yes: function(index) {
											layer.close(index);
											$('#wa_fb').click()
										}
									});
								}
							}
							num++;
						}, 200)
					}
					//回填
					if (info.article.title) {
						$('.wabt_text').html(info.article.title); //文章标题
						document.getElementsByTagName("title")[0].innerText = info.article.title;
					};

					if (info.article.content) {
						var list = JSON.parse(info.article.content);
						if (list.length > 0) {
							var html = '';
							list.forEach(function(o, index) {
								if (o.content) {
									if (o.type == 'text') {
										html += '<div><pre>' + o.content + '</pre></div>';
									};
									if (o.type == 'img') {
										html += '<div><img src="' + o.content + '?x-oss-process=image/auto-orient,1"/></div>';
									};
								};
							});
							$('#article-body1').html(html); //文章内容
						};
					};

					if (info.callCard) {
						if (info.callCard.cpyNick) {
							$('.data_cpyNick').html(info.callCard.cpyNick); //公司昵称
							$('.user_cen').addClass('isgs');
						} else {
							$('.userInfo_gs').hide();
						};
						if (info.callCard.job) {
							$('.data_job').html(info.callCard.job); //职位
						} else {
							$('.userInfo_gs').hide();
						};
						if (info.callCard.phone) {
							$('#data_phone').html(info.callCard.phone); //手机号
						};
						if (info.callCard.qq) {
							$('#data_qq').html(info.callCard.qq); //qq
						} else {
							$('.qqh').hide();
						};
						if (info.callCard.email) {
							$('#data_email').html(info.callCard.email); //邮箱
						} else {
							$('.yxh').hide();
						};
						if (info.callCard.address) {
							$('#data_address').html(info.callCard.address); //地址
						} else {
							$('.dzh').hide();
						};
						if (info.callCard.url) {
							$('#data_url').html(info.callCard.url); //网址
						} else {
							$('.wzh').hide();
						};
						if (info.callCard.cpySummary) {
							$('#data_cpySummary').html(info.callCard.cpySummary); //公司简介
						} else {
							$('#data_cpySummary').hide();
						};
						if (info.callCard.weChat) {
							$('#data_weChat').html(info.callCard.weChat); //微信号
						} else {
							$('.wxh').hide();
						};
					}
				};
			}, function() {});
		};
	};

	//联系他点击
	$('.user_lx').click(function() {
		$('html, body').animate({
			scrollTop: $("#userimg").offset().top
		}, 300);
	});

	//打电话
	$('#callphone').click(function() {
		if (info && info.callCard && info.callCard.phone) {
			window.location.href = 'tel://' + info.callCard.phone;
		};
	});

	//加微信
	$('#addweixin').click(function() {
		var flg = copyText(info.callCard.weChat);
		if (flg) {
			layer.msg('微信号已复制', {
				time: 1500
			});
		};
	});

	//免费换成我的文案
	$('#wa_fb').click(function() {
		if (userInfo) {
			if (articleid && info) {
				//判断采集
				if (userInfo.collectSubscribe == 1) {
					// 是否关注
					if (userInfo.subscribe == 1) {
						//判断按钮类型
						if (info.article.userAccountId == userInfo.id) {
							//用户编辑
							var data = {
								type: 1,
								articleid: articleid
							};
							window.location.href = ymym + "article_edit.html?params=" + encodeURIComponent(JSON.stringify(data));
						} else {
							//换成我的
							if (!mfhcwoFlg && !mfhcwoPost) {
								mfhcwoPost = true;
								postF2(jkMap.articlecopy, {
									articleId: articleid,
									userAccountId: userInfo.id
								}, 'post', 'json', 'application/x-www-form-urlencoded', function(res) {
									if (res.result.success && res.data.articleId) {
										mfhcwoFlg = true;
										var data = {
											type: 2,
											articleid: res.data.articleId
										};
										window.location.href = ymym + "article_edit.html?params=" + encodeURIComponent(JSON.stringify(data));
									} else {
										layer.msg(res.result.message, {
											time: 2000
										});
									};
									mfhcwoPost = false;
								}, function() {
									mfhcwoPost = false;
								});
							} else {
								if (mfhcwoPost) {
									layer.msg('正在操作中，请勿重复点击', {
										time: 2000
									});
								};
							};
						};
					} else {
						//未关注
						var msg = '关注公众号，体验更好的服务！'; //编辑提示语
						if (pageScene == 1) {
							msg = '关注公众号，享受免费权限！'; //免费换成我的提示语
						};
						var bw = $('body').width();
						layer.msg(msg, {
							skin: 'gztc-class',
							area: Number(bw * 0.7) + 'px',
							time: 0, //不自动关闭							
							btn: ['关注', '取消'],
							yes: function(index) {
								layer.close(index);
								//弹出关注二维码
								focusDivTc();
							}
						});
					};
				} else {
					//未采集
					// layer.msg('网络繁忙，请刷新页面重试',{time:2000});
					//采集关注信息
					focusCJ(userInfo.userId, function(res) {
						if (res.result.success) {
							userInfo.subscribe = res.data.subscribe ? 1 : 0;
							userInfo.collectSubscribe = 1;
							//设置用户缓存
							set_cache('userInfo', userInfo);
							$('#wa_fb').click();
						};
					}, function() {});
				};
			} else {
				layer.msg('信息有误，请刷新页面', {
					time: 2000
				});
			};
		} else {
			layer.msg('未登录,请刷新页面重新授权', {
				time: 2000
			});
		};
	});

	//我的发布
	$('#tolist').click(function() {
		window.location.href = ymym + "article_list.html";
	});

	//分享隐藏
	$('#shareTs').click(function() {
		$(this).hide();
	});

})()
